<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统基础管理</el-breadcrumb-item>
      <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-row :gutter="10">
        <el-col span="4">
          <el-button type="primary" icon="el-icon-plus" @click="addmeal"
            >新增</el-button
          >
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table
        :data="treeList"
        style="width: 100%; margin-bottom: 20px"
        row-key="id"
        border
        :tree-props="{ children: 'children' }"
        stripe
      >
        <el-table-column prop="name" label="菜单名称" width="200">
        </el-table-column>
        <el-table-column prop="type" label="菜单类型" width="180">
        </el-table-column>
        <el-table-column prop="resKey" label="唯一KEY"> </el-table-column>
        <el-table-column prop="resUrl" label="URL地址"> </el-table-column>
        <el-table-column prop="ishide" label="是否隐藏">
          <template #default="scope">
            <span v-if="scope.row.ishide == 0">否</span>
            <span v-if="scope.row.ishide == 1">是</span>
          </template>
        </el-table-column>
        <el-table-column prop="description" label="描述" show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="操作" width="120" align="center" fixed="right">
          <template #default="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editDialog(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="del(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 添加对话框 -->
      <el-dialog title="新增" v-model="mealDV" width="40%">
        <!-- 对话框内容 -->
        <el-form
          :model="addForm"
          status-icon
          :rules="addFormrules"
          ref="addFormRef"
          label-width="135px"
        >
          <el-form-item label="菜单名称" prop="name">
            <el-input
              v-model="addForm.name"
              placeholder="请输入菜单名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="菜单标识" prop="resKey">
            <el-input
              v-model="addForm.resKey"
              placeholder="请输入菜单标识"
            ></el-input>
          </el-form-item>
          <el-form-item label="菜单URL" prop="resUrl">
            <el-input
              v-model="addForm.resUrl"
              placeholder="请输入菜单URL"
            ></el-input>
          </el-form-item>
          <el-form-item label="上级菜单" prop="parentId">
            <el-cascader
              v-model="addForm.parentId"
              :options="treeList"
              :props="{ checkStrictly: true,
              children:'children',
               label:'name',
               value:'id'}"
              clearable
            ></el-cascader>
          </el-form-item>
          <el-form-item label="菜单类型" prop="type">
            <el-select v-model="addForm.type" placeholder="请选择菜单类型">
              <el-option label="-目录-" value="0"></el-option>
              <el-option label="-菜单-" value="1"></el-option>
              <el-option label="-按钮-" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="图标" prop="icon">
            <el-input
              v-model="addForm.icon"
              placeholder="请输入图标"
            ></el-input>
          </el-form-item>
          <el-form-item label="是否隐藏" prop="ishide">
            <el-select v-model="addForm.ishide" placeholder="请选择是否隐藏">
              <el-option label="是" value="1"></el-option>
              <el-option label="否" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="菜单描述" prop="description">
            <el-input
              v-model="addForm.description"
              placeholder="请输入菜单描述"
            ></el-input>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="mealDV = false">取 消</el-button>
            <el-button type="primary" @click="add">确 定</el-button>
          </span>
        </template>
      </el-dialog>
      <!-- 编辑对话框 -->
      <el-dialog title="编辑" v-model="editmealDV" width="40%">
        <!-- 对话框内容 -->
        <el-form
          :model="editForm"
          status-icon
          :rules="addFormrules"
          ref="editFormRef"
          label-width="135px"
        >
          <el-form-item label="菜单名称" prop="name">
            <el-input
              v-model="editForm.name"
              placeholder="请输入菜单名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="菜单标识" prop="resKey">
            <el-input
              v-model="editForm.resKey"
              placeholder="请输入菜单标识"
            ></el-input>
          </el-form-item>
          <el-form-item label="菜单URL" prop="resUrl">
            <el-input
              v-model="editForm.resUrl"
              placeholder="请输入菜单URL"
            ></el-input>
          </el-form-item>
          <el-form-item label="上级菜单" prop="parentId">
            <el-cascader
              v-model="editForm.parentId"
              :options="treeList"
              :props="{ checkStrictly: true,
              children:'children',
               label:'name',
               value:'id'}"
              clearable,
            ></el-cascader>
          </el-form-item>
          <el-form-item label="菜单类型" prop="type">
            <el-select v-model="editForm.type" placeholder="请选择菜单类型">
              <el-option label="-目录-" value="0"></el-option>
              <el-option label="-菜单-" value="1"></el-option>
              <el-option label="-按钮-" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="图标" prop="icon">
            <el-input
              v-model="editForm.icon"
              placeholder="请输入图标"
            ></el-input>
          </el-form-item>
          <el-form-item label="是否隐藏" prop="ishide">
            <el-select v-model="editForm.ishide" placeholder="请选择是否隐藏">
              <el-option label="是" value="1"></el-option>
              <el-option label="否" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="菜单描述" prop="description">
            <el-input
              v-model="editForm.description"
              placeholder="请输入菜单描述"
            ></el-input>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="editmealDV = false">取 消</el-button>
            <el-button type="primary" @click="edit">确 定</el-button>
          </span>
        </template>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      treeList: [],
      mealDV: false,
      editmealDV: false,
      addForm: {
        name: "",
        resKey: "",
        resUrl: "",
        parentId: "0",
        type: "",
        icon: "",
        ishide: "",
        description: "",
      },
      editForm: {parentId: "0",},
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      let fromdata = new FormData();
      axios.post("/IE_MES/resources/treelists.shtml", fromdata).then((res) => {
        console.log(res.data);
        this.treeList = res.data.treelists;
      });
    },
    addmeal() {
      this.mealDV = true;
      this.$axios({
        headers: {
          "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        },
        method: "post",
        url: "/IE_MES/resources/reslists.shtml?_t=" + new Date(),
      }).then((res) => {
        if (res.data == "success") {
          console.log(res.data);
        }
      });
    },
    // 新增
    add() {
      this.$axios({
        headers: {
          "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        },
        method: "post",
        url: "/IE_MES/resources/addEntity.shtml",
        data: this.$qs.stringify({
          "resFormMap.name": this.addForm.name,
          "resFormMap.resKey": this.addForm.resKey,
          "resFormMap.resUrl": this.addForm.resUrl,
          "resFormMap.parentId": this.addForm.parentId,
          "resFormMap.type": this.addForm.type,
          "resFormMap.icon": this.addForm.icon,
          "resFormMap.ishide": this.addForm.ishide,
          "resFormMap.description": this.addForm.description,
        }, {arrayFormat: 'repeat'}),
      }).then((res) => {
        if (res.data == "success") {
          this.getList();
          this.$refs["addFormRef"].resetFields();
          this.mealDV = false;
          this.$message({
            type: "success",
            message: "添加成功!",
          });
        }
      });
    },
    // 编辑
    editDialog(row) {
      this.editForm = JSON.parse(JSON.stringify(row));
      console.log(JSON.parse(JSON.stringify(row)));
      this.editmealDV = true;
    },
    edit() {
      this.$axios({
        headers: {
          "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        },
        method: "post",
        url: "/IE_MES/resources/editEntity.shtml",
        data: this.$qs.stringify({
          "resFormMap.id": this.editForm.id,
          "resFormMap.name": this.editForm.name,
          "resFormMap.resKey": this.editForm.resKey,
          "resFormMap.resUrl": this.editForm.resUrl,
          "resFormMap.parentId": this.editForm.parentId,
          "resFormMap.type": this.editForm.type,
          "resFormMap.icon": this.editForm.icon,
          "resFormMap.ishide": this.editForm.ishide,
          "resFormMap.description": this.editForm.description,
        }),
      }).then((res) => {
        if (res.data == "success") {
          this.getList();
          this.editmealDV = false;
          this.$message({
            type: "success",
            message: "编辑成功!",
          });
        }
      });
    },
    // 删除
    del(row) {
      this.$confirm("此操作将永久删除该内容, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios({
            headers: {
              "Content-Type":
                "application/x-www-form-urlencoded; charset=UTF-8",
            },
            method: "post",
            url: "/IE_MES/resources/deleteEntity.shtml?_t=" + new Date(),
            data: this.$qs.stringify({
              ids: row.id,
            }),
          }).then((res) => {
            if (res.data == "success") {
              this.getList();
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style lang="less" scoped>
</style>
